/**
 * 获取页面参数hooks
 * //!!!!!注意，页面参数必须在onMounted及之后的生命周期中、或者事件方法中获取，否则获取不到
 * 调用
 *  ```js
 *  const {query} = useGetPageQuery();
 *  onShow(()=>{
 *    console.log(query.value)
 *  })
 * ```
 */

import { onLoad } from "@dcloudio/uni-app";
import { ref } from "vue";

export function useGetPageQuery() {
  const query = ref({});
  onLoad((options) => {
    //如果是小程序分享码扫进来的，抹平scene和query
    if (options.scene) {
      const scene = decodeURIComponent(options.scene);
      const sceneArr = scene.split("&");
      sceneArr.forEach((item) => {
        const itemArr = item.split("=");
        options[itemArr[0]] = itemArr[1];
      });
    }
    delete options.scene;
    query.value = options;
  });
  return {
    query,
  };
}
